package com.temp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.temp.dto.SearchDonationDTO;
import com.temp.entity.Donation;
import com.temp.mapper.DonationMapper;
import com.temp.service.DonationService;
import com.temp.vo.CustomPageVO;
import com.temp.vo.DonationDateVO;
import com.temp.vo.DonationUserVO;
import com.temp.vo.DonationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class DonationServiceImpl extends ServiceImpl<DonationMapper, Donation> implements DonationService {

    @Autowired
    private DonationMapper donationMapper;

    @Override
    public CustomPageVO<DonationUserVO> findAllByCampaign(SearchDonationDTO donationDTO) {
        CustomPageVO<DonationUserVO> customPageVO = new CustomPageVO<>();
        Integer count = Optional.ofNullable(donationMapper.countAllByCampaign(donationDTO)).orElse(0);
        customPageVO.setTotal(count);
        if(count < 1) {
            customPageVO.setDataList(Collections.emptyList());
            return customPageVO;
        }

        List<DonationUserVO> donations = donationMapper.findAllByCampaign(donationDTO);
        int size = donations.size();
        for (int i = 0; i < size; i++) {
            DonationUserVO user = donations.get(i);
            String username = user.getDonorName();

            if (username != null) {
                int strLen = username.length();
                if (strLen > 0) {
                    if (strLen == 1) {
                        user.setDonorName("*");
                    } else {
                        char[] chars = new char[strLen];
                        chars[0] = username.charAt(0);
                        for (int j = 1; j < strLen - 1; j++) {
                            chars[j] = '*';
                        }
                        chars[strLen - 1] = username.charAt(strLen - 1);
                        user.setDonorName(new String(chars));
                    }
                }
            }
        }
        customPageVO.setDataList(donations);
        return customPageVO;
    }

    @Override
    public BigDecimal countDonationByRobot(Long campaignId) {
        return donationMapper.countDonationByRobot(campaignId);
    }

    @Override
    public CustomPageVO<Donation> findAllUserDonation(SearchDonationDTO donationDTO) {
        CustomPageVO<Donation> customPageVO = new CustomPageVO<>();
        Integer count = Optional.ofNullable(donationMapper.countAllUserDonations(donationDTO)).orElse(0);
        customPageVO.setTotal(count);
        if(count < 1) {
            customPageVO.setDataList(Collections.emptyList());
            return customPageVO;
        }

        List<Donation> donations = donationMapper.findAllUserDonations(donationDTO);
        customPageVO.setDataList(donations);
        return customPageVO;
    }

    @Override
    public DonationVO countOneSelfDonations(Long userId) {
        DonationVO donationVO = donationMapper.countOneSelfDonations(userId);
        donationVO.setAmount(Optional.ofNullable(donationVO.getAmount()).orElse(BigDecimal.ZERO));
        return donationVO;
    }

    @Override
    public List<DonationDateVO> countDonationDate(Long campaignId) {
        return donationMapper.countDonationDate(campaignId);
    }
}
